import java.io.BufferedReader;
import java.io.InputStreamReader;

import edu.ucla.cs.typecast.discovery.ServiceAdvertisement;
import edu.ucla.cs.typecast.discovery.ServiceDiscoveryClient;
import edu.ucla.cs.typecast.discovery.ServiceDiscoveryListener;
import edu.ucla.cs.typecast.discovery.ServiceDiscoveryManager;
import edu.ucla.cs.typecast.discovery.ServiceDiscoveryService;
import edu.ucla.cs.typecast.discovery.ServiceTemplate;
import edu.ucla.cs.typecast.rmi.Future;

/**************************************************************************
 *
 * Copyright (C) 2007 University of California, Los Angeles.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *************************************************************************/

/**
 * 
 */

/**
 *
 * @date Sep 20, 2007
 */
public class Discovery2 {
	  public static void main(String[] args) { 
	        try { 
	            Class type = Class.forName(args[0]); 
	            Class[] types = new Class[] { type }; 
	            ServiceTemplate template = new ServiceTemplate(types); 
	            ServiceDiscoveryListener listener = new ServiceDiscoveryListener() { 
	                public void notify(ServiceAdvertisement serviceInfo) { 
	                    System.out.print("Discovered Services: "  +  
	                            "Host=" + serviceInfo.getHost() + ", GUID=" + serviceInfo.getGUID()); 
	                    System.out.print(", Type=["); 
	                    for(int i=0; i<serviceInfo.getTypes().length; i++) { 
	                        System.out.print(serviceInfo.getTypes()[i].getName() + " "); 
	                    }
	                    System.out.print("], EndPoint=["); 
	                    for(int i=0; i<serviceInfo.getEndPoints().length; i++) { 
	                        System.out.print(serviceInfo.getEndPoints()[i] + " "); 
	                    }
	                    System.out.println("]"); 
	                }
	            }; 
	            ServiceDiscoveryManager.getInstance().addDiscoveryListener(template, listener); 
	            ServiceDiscoveryService client = ServiceDiscoveryManager.getInstance().getDiscoveryClient(); 
	            
	            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
	            while(true){ 
		            Future<ServiceAdvertisement[]> result = client.discoverWithUnicastReturn(types, template); 
		            System.out.print("Enter to print the result"); 
	                reader.readLine(); 
	                result.freeze(); 
	                for(int i=0; i<result.getCounts(); i++) { 
	                	for(int j=0; j<result.get(i).length; j++) { 
	                		print(result.get(i)[j]);
	                	}
	                }
	            }                
	        } catch(Throwable t) { 
	            t.printStackTrace(); 
	        }
	    }
	  
	  private static void print(ServiceAdvertisement serviceInfo) { 
		  System.out.print("Discovered Services: "  +  
                  "Host=" + serviceInfo.getHost() + ", GUID=" + serviceInfo.getGUID()); 
          System.out.print(", Type=["); 
          for(int i=0; i<serviceInfo.getTypes().length; i++) { 
              System.out.print(serviceInfo.getTypes()[i].getName() + " "); 
          }
          System.out.print("], EndPoint=["); 
          for(int i=0; i<serviceInfo.getEndPoints().length; i++) { 
              System.out.print(serviceInfo.getEndPoints()[i] + " "); 
          }
          System.out.println("]"); 
	  }
}
